//
//  YYUploadVC.m
//  drivingSchool
//
//  Created by 子yu 刘 on 2018/8/18.
//  Copyright © 2018年 子yu 刘. All rights reserved.
//

#import "YYUploadVC.h"

@interface YYUploadVC ()<UIAlertViewDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (nonatomic,copy)NSString * imageName;

@end

@implementation YYUploadVC{
    UIImagePickerController * _imagePicker;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.btnImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",DEF_INTERFACE_WEBVIEWSERVICE,self.urlStr]] forState:UIControlStateNormal placeholderImage:DEF_IMAGENAME(@"up_photo")];
    // Do any additional setup after loading the view.
}

- (IBAction)TakingPictures:(id)sender {
    [ASSheetViewController setSheetControllerWithTipTitle:nil message:nil itemsArray:@[@"拍照",@"从相册选择"] cancelTitle:@"取消" viewController:self handler:^(NSInteger index) {
        if (index == 1) {
            [self addImageFromCameraOrFromAlbum:UIImagePickerControllerSourceTypeCamera];
        }else{
            [self addImageFromCameraOrFromAlbum:UIImagePickerControllerSourceTypePhotoLibrary];
        }
    }];
}

- (IBAction)upload:(id)sender {
    if (_block) {
        self.block(_imageName);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

//添加图片或者拍照
- (void)addImageFromCameraOrFromAlbum:(UIImagePickerControllerSourceType)type{
    _imagePicker = [[UIImagePickerController alloc] init];
    _imagePicker.delegate = self;
    _imagePicker.sourceType = type;
    _imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    _imagePicker.allowsEditing = YES;
    //    if (IOS7_0) {
    //        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    //    }
    [self presentViewController:_imagePicker animated:YES completion:nil];
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    // bug fixes: UIIMagePickerController使用中偷换StatusBar颜色的问题
    if ([navigationController isKindOfClass:[UIImagePickerController class]] &&
        ((UIImagePickerController *)navigationController).sourceType ==     UIImagePickerControllerSourceTypePhotoLibrary) {
        [[UIApplication sharedApplication] setStatusBarHidden:NO];
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
    }
}

//得到拍下来的照片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // 得到照片信息
    // UIImage *images=[info objectForKey:UIImagePickerControllerOriginalImage];
    NSString* mediaType=[info objectForKey:UIImagePickerControllerMediaType];
    //得到图片是否为刚拍照的图片
    if([mediaType isEqualToString:@"public.image"])
    {
        UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
        
        // 把头像图片存到本地
        NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
        NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"1.jpg"]];   // 保存文件的名称
        NSData *imageData = UIImageJPEGRepresentation(image, 0.2);
        [imageData writeToFile:filePath atomically:YES];
        [_imagePicker dismissViewControllerAnimated:YES completion:nil];
        
        // NSString * encodeResult = [imageData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
        NSData *filePathData = [NSData dataWithContentsOfFile:filePath];
        [AlertUtil showMessage:nil toView:self.view];
        [RequestOperationManager personalUploadImg_image:@"1.jpg" imageData:filePathData success:^(id result) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            if (DEF_REQ_SUCCESS) {
                [self.btnImage setImage:image forState:UIControlStateNormal];
                self.imageName = result[@"data"];
            }
            
        } failture:^(NSError *error) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
        
    }
}



@end
